﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Baopx_API;

namespace Baopx.BackEnd.ManagePage
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        protected void BindData()
        {
            grdPage.DataSource = A.Controller.BackEndPageController.GetAllPage();
            grdPage.DataBind();
        }

        protected void grdPage_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var cboCategory = (DropDownList)e.Item.FindControl("cboCategory");
                cboCategory.DataSource = A.Controller.BackEndMenuController.GetAllBackEndMenuChild();
                cboCategory.DataTextField = "Name";
                cboCategory.DataValueField = "Id";
                cboCategory.DataBind();
                cboCategory.SelectedValue = DataBinder.Eval(e.Item.DataItem, "MenuId").ToString();
            }
            if (e.Item.ItemType == ListItemType.Footer)
            {
                var cboCategory = (DropDownList)e.Item.FindControl("cboCategory");
                cboCategory.DataSource = A.Controller.BackEndMenuController.GetAllBackEndMenuChild();
                cboCategory.DataTextField = "Name";
                cboCategory.DataValueField = "Id";
                cboCategory.DataBind();
            }
        }

        protected void grdPage_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "cmdAddNew":
                    BackEndPage backEndPageNew = new BackEndPage();
                    var txtName = (TextBox) e.Item.FindControl("txtName");
                    var txtAbsolutePath = (TextBox) e.Item.FindControl("txtAbsolutePath");
                    var txtOrderDisplay = (TextBox) e.Item.FindControl("txtOrderDisplay");
                    var cboCategory = (DropDownList) e.Item.FindControl("cboCategory");

                    backEndPageNew.MenuId = Convert.ToInt32(cboCategory.SelectedValue);
                    backEndPageNew.Name = txtName.Text;
                    backEndPageNew.AbsolutePath = txtAbsolutePath.Text;
                    try
                    {
                        backEndPageNew.OrderDisplay = Convert.ToInt32(txtOrderDisplay.Text);
                    }
                    catch (Exception)
                    {
                        backEndPageNew.OrderDisplay = 1;
                    }
                    A.Controller.BackEndPageController.Insert(backEndPageNew);
                    break;
                case "cmdUpdate":
                    BackEndPage backEndPageUpdate = A.Controller.BackEndPageController.GetElementById(Convert.ToInt32(e.CommandArgument));
                    var txtName1 = (TextBox) e.Item.FindControl("txtName");
                    var txtAbsolutePath1 = (TextBox) e.Item.FindControl("txtAbsolutePath");
                    var txtOrderDisplay1 = (TextBox) e.Item.FindControl("txtOrderDisplay");
                    var cboCategory1 = (DropDownList) e.Item.FindControl("cboCategory");
                    var chkActvie = (CheckBox) e.Item.FindControl("chkActive");
                    backEndPageUpdate.IsActive = chkActvie.Checked;

                    backEndPageUpdate.MenuId = Convert.ToInt32(cboCategory1.SelectedValue);
                    backEndPageUpdate.Name = txtName1.Text;
                    backEndPageUpdate.AbsolutePath = txtAbsolutePath1.Text;
                    try
                    {
                        backEndPageUpdate.OrderDisplay = Convert.ToInt32(txtOrderDisplay1.Text);
                    }
                    catch (Exception)
                    {
                    }
                    A.Controller.BackEndPageController.Update();
                    break;
                case "cmdDelete":
                    BackEndPage backEndPageDelete = A.Controller.BackEndPageController.GetElementById(Convert.ToInt32(e.CommandArgument));
                    A.Controller.BackEndPageController.Delete(backEndPageDelete);
                    break;
            }
            BindData();
        }
    }
}